<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Rap2hpoutre\FastExcel\FastExcel;

class TJBDC extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'TJBDC';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(Request $request)
    {
        $data = $this->getData('bdc3');

        $class = 'App\Admin\Controllers\JWtUser';

        foreach ($data as $k => $v) {

            $tokenO = trim($v['token']);

//            $tokenO = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJTSEEyNTYifQ.eyJ1aWQiOjc2LCJ0ZXJtaW5hbCI6IndlYkNoYXQiLCJleHAiOjE3MDU5NzYxNjl9.ad2d7a02130154e7e0c4eb3d59b1688f12cc51fb86bc27407a03a35751e3ebe6';

            $request->merge(['token' => substr($tokenO, 7)]);

            $res = call_user_func_array([new $class, 'getUserByToken'], [$request]);

            dump($tokenO, $res);
            $uid = $res['result']['user_id'] ?? '';
            $uname = $res['result']['username'] ?? '';

            DB::table('jwttoken2user')->insertOrIgnore([
                'token'    => $tokenO,
                'uid'      => $uid,
                'username' => $uname,
            ]);
        }

        return 0;
    }

    protected function getData($name)
    {
        $collection = (new FastExcel())->import(storage_path()."/".$name.".csv");
        return $collection;
    }

    public function decoJwt($data)
    {


    }
}
